/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * ProfilesTableModel.java
 *
 * Created on April 22, 2007, 3:35 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.decamps.m2settings.model;

import info.decamps.m2settings.XMLBinder;
import java.io.IOException;
import javax.swing.event.TableModelEvent;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

/**
 *
 * @author regis
 */
public class ProfilesTableModel extends ExtendableTableModel {
    private Profiles profiles;
    
    public static final int COLUMN_ID=0;
    public static final int COLUMN_ACTIVATION=1;
    public static final int COLUMN_PROPERTIES=2;
    public static final int COLUMN_REPOSITORIES=3;
    public static final int COLUMN_PLUGINREPOSITORIES=4;
    
    /** Creates a new instance of ProfilesTableModel */
    public ProfilesTableModel(Profiles profiles) {
        if (profiles==null) {
            profiles=new Profiles();
        }
        this.profiles=profiles;
        columnTitles=new String[5];
        columnTitles[COLUMN_ID]="id";
        columnTitles[COLUMN_ACTIVATION]="activation";
        columnTitles[COLUMN_PROPERTIES]="property";
        columnTitles[COLUMN_REPOSITORIES]="repository";
        columnTitles[COLUMN_PLUGINREPOSITORIES]="plugin repository";
    }
    
    public void removeRows(int[] rowIndexes) {
        for (int i = rowIndexes.length - 1; i >= 0; i--) {
            Profile vProfile=getRow(rowIndexes[i]);
            profiles.removeProfile(vProfile);
        }
        fireTableChanged(new TableModelEvent(this));
    }
    
    public String getElementName() {
        return "profile";
    }
    
    public int getRowCount() {
        return profiles.getProfileCount();
    }
    
    public Object getValueAt(int rowIndex, int columnIndex) {
        Profile row= getRow(rowIndex);
        switch (columnIndex) {
            case COLUMN_ID: return row.getId();
            case COLUMN_ACTIVATION: return activationToString(row.getActivation());
            case COLUMN_PROPERTIES: return propertiesToString(row.getProperties());
            case COLUMN_REPOSITORIES: return repositoriesToString(row.getRepositories());
            case COLUMN_PLUGINREPOSITORIES: return repositoriesToString(row.getPluginRepositories());
        }
        return null;
    }
    
    public Profile getRow(int rowIndex) {
        return profiles.getProfile(rowIndex);
    }
    
    public void addRow(Profile vProfile) {
        profiles.addProfile(vProfile);
    }
    
    private String repositoriesToString(Repositories repositories) {
        if (repositories==null)
            return "-";
        return repositoriesToString(repositories.getRepository());
    }
    private String repositoriesToString(PluginRepositories pluginRepositories) {
        if (pluginRepositories==null) {
            return "-";
        }
        return repositoriesToString(pluginRepositories.getPluginRepository());
    }
    private String repositoriesToString(RepositoryPolicyType[] repos) {
        if (repos==null || repos.length<1) {
            return "-";
        }                
        String retval="";
        for(int i=0; i<repos.length; i++) {
            retval+=repos[i].getId()+" ; ";
        }
        return retval;
    }
    
    private String activationToString(Activation activation) {
        if (activation==null)
            return "-";
        if (activation.getActiveByDefault()) {
            return "default";
        }
        String retval="";
        Property prop=activation.getProperty();
        if (prop!=null) {
            retval+=prop.getName()+"="+prop.getValue()+" ";
        }
        String jdk=activation.getJdk();
        if (jdk!=null) {
            retval+="JDK ("+jdk+") ";
        }
        if (activation.getOs()!=null){
            retval="OS ("+activation.getOs().getName()+") ";
        }        
        return null;
    }

    private String propertiesToString(Properties properties) {
        if (properties==null) {
            return "-";
        }
        String retval;
        try {
            retval= XMLBinder.toXMLString(properties);
        } catch (ValidationException ex) {
            ex.printStackTrace();
            retval=ex.getMessage();
        } catch (MarshalException ex) {
            ex.printStackTrace();
            retval=ex.getMessage();
        } catch (IOException ex) {
            ex.printStackTrace();
            retval=ex.getMessage();
        }
        return retval;
    }

    public Profiles getProfiles() {
        return profiles;
    }
}